package com.ego.item.controller;

import com.ego.item.pojo.Specification;
import com.ego.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("spec")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @GetMapping("{cid}")
    public ResponseEntity<String> querySpecificationByCategoryId(@PathVariable("cid") Long cid) {
        Specification spec = this.specificationService.findSpecificationsByCid(cid);
        if (spec == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(spec.getSpecifications());
    }

    @PostMapping
    public ResponseEntity<Void> addSpecification(Specification specification) {
        this.specificationService.addSpecification(specification);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateSpecification(Specification specification) {
        this.specificationService.updateSpecification(specification);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
